package com.zodiac.app.szse;

import java.util.List;

public enum FinancialType {
    // 资产负债表
    FA(".+合并资产负债表$") {
        @Override
        List<String> findFinancialContent(List<String> rawContent) {
            int size = rawContent.size();
            int startLineNo = 0;
            int endLineNo = 0;
            boolean start = false;
            for (int i = 0; i < size; i++) {
                String line = rawContent.get(i).trim();
                if (!start && line.matches(getPattern())) {
                    start = true;
                }
                if (!start) {
                    continue;
                }
                if (line.equals("流动资产：")) {
                    startLineNo = i;
                    continue;
                }
                if (line.matches("^负债.+总计\\s+.+")) {
                    endLineNo = i;
                    break;
                }
            }
            if (0 == startLineNo || 0 == endLineNo) {
                throw new FinancialDataExtractException(this, rawContent);
            }
            return rawContent.subList(startLineNo, endLineNo + 1);
        }
    },
    // 利润表
    FB(".+合并年初到报告期末利润表$") {
        @Override
        List<String> findFinancialContent(List<String> rawContent) {
            int size = rawContent.size();
            int startLineNo = 0;
            int endLineNo = 0;
            boolean start = false;
            for (int i = 0; i < size; i++) {
                String line = rawContent.get(i).trim();
                if (!start && line.matches(getPattern())) {
                    start = true;
                }
                if (!start) {
                    continue;
                }
                if (line.matches("项目\\s+本期发生额\\s+上期发生额")) {
                    startLineNo = i + 1;
                    continue;
                }
                if (line.contains("稀释每股收益")) {
                    endLineNo = i;
                    break;
                }
                if (line.contains("法定代表人") || line.contains("母公司年初到报告期末利润表")) {
                    endLineNo = i - 1;
                    break;
                }
            }
            if (0 == startLineNo || 0 == endLineNo) {
                throw new FinancialDataExtractException(this, rawContent);
            }
            return rawContent.subList(startLineNo, endLineNo + 1);
        }
    },
    // 现金流量表
    FC(".+合并年初到报告期末现金流量表$") {
        @Override
        List<String> findFinancialContent(List<String> rawContent) {
            int size = rawContent.size();
            int startLineNo = 0;
            int endLineNo = 0;
            boolean start = false;
            for (int i = 0; i < size; i++) {
                String line = rawContent.get(i).trim();
                if (!start && line.matches(getPattern())) {
                    start = true;
                }
                if (!start) {
                    continue;
                }
                if (line.matches("项目\\s+本期发生额\\s+上期发生额")) {
                    startLineNo = i + 1;
                    continue;
                }
                if (line.contains("期末现金及现金等价物余额")) {
                    endLineNo = i;
                    break;
                }
                if (line.contains("法定代表人") || line.contains("母公司年初到报告期末现金流量表")) {
                    endLineNo = i - 1;
                    break;
                }
            }
            if (0 == startLineNo || 0 == endLineNo) {
                throw new FinancialDataExtractException(this, rawContent);
            }
            return rawContent.subList(startLineNo, endLineNo + 1);
        }
    };

    private final String pattern;

    FinancialType(String pattern) {
        this.pattern = pattern;
    }

    public String getPattern() {
        return pattern;
    }

    /**
     * 找到财务报表相关的内容
     *
     * @param rawContent 原始报告内容
     * @return
     */
    abstract List<String> findFinancialContent(List<String> rawContent);
}
